class Difference  

    def self.method_missing(method_name,connection)
        method_name_array = method_name.to_s.split("_")
        first_table_name = (method_name_array[(method_name_array.index("for")+1)..(method_name_array.index("and")-1)]).join("_")
        second_table_name = (method_name_array[(method_name_array.index("and")+1)..(method_name_array.index("under")-1)]).join("_")		
        Difference.check_compatibility(first_table_name,connection,second_table_name,connection)
    end



    def self.check_compatibility(tablename1,connection1,tablename2,connection2)
        overall_complete_compatibility = 0
        overall_namewise_compatibility = 0
        overall_structure_compatibility = 0
        match_found = []
        $table_compatibility_array = []
        column_count_one = connection1.execute("desc #{tablename1}")
        columns_size = get_column_list(connection1,tablename1).size
        File.makedirs("#{tablename1}") 
        column_count_one.each{|first_column_value|
            g = Gruff::Bar.new
            g.title = "My Graph" 
            compatibility_index = 0
            name_compatibility_index = 0
            completely_compatibility_index = 0
            column_count_two = connection2.execute("desc #{tablename2}")
            column_count_two.each{|second_column_value|
                if first_column_value[1..(first_column_value.length)-1] == second_column_value[1..(second_column_value.length)-1]
                    if first_column_value[0] == second_column_value[0]
                        completely_compatibility_index = completely_compatibility_index + 1
                        match_found = match_found + [first_column_value[0]] 
                    else    
                        compatibility_index = compatibility_index + 1
                        match_found = match_found + [first_column_value[0]]
                    end   
                elsif first_column_value[0] == second_column_value[0]
                    if !match_found.include?first_column_value[0]
                        name_compatibility_index = name_compatibility_index + 1
                        match_found = match_found + [first_column_value[0]] 
                    end    
                end	
            }
            overall_complete_compatibility = overall_complete_compatibility + completely_compatibility_index
            overall_namewise_compatibility = overall_namewise_compatibility + name_compatibility_index
            overall_structure_compatibility = overall_structure_compatibility + compatibility_index
            if overall_complete_compatibility == 0 and  overall_namewise_compatibility == 0 and overall_structure_compatibility == 0
                  puts "#{tablename1} and #{tablename2} are not compatible ta all."
                  return
            else      
                g.data("fully compatible with", [completely_compatibility_index])
                g.data("name wise compatible with", [name_compatibility_index])
                g.data("structurly compatible with", [compatibility_index])
                g.labels = {1 => 'fully', 4 => 'name', 8 => 'structurly'}
                g.write("#{tablename1}/#{first_column_value[0]}.png")
            end
        } 
        g_table = Gruff::Bar.new
        g_table.title = "My Graph"   
        g_table.data("fully compatible with", [overall_complete_compatibility])
        g_table.data("name wise compatible with", [overall_namewise_compatibility])
        g_table.data("structurly compatible with", [overall_structure_compatibility])
        g_table.labels = {1 => 'fully', 2 => 'name', 4 => 'structurly'}
        g_table.write("#{tablename1}/#{tablename1}_full_compatibility_level_2.png")
        g_table_full = Gruff::Bar.new
        g_table_full.title = "My Graph"   
        g_table_full.data("fully compatible with", [overall_complete_compatibility])
        g_table_full.data("not compatible ", [(columns_size - overall_complete_compatibility)])
        g_table_full.labels = {1 => 'fully compatible', 2 => 'not fully compatible'}
        g_table_full.write("#{tablename1}/#{tablename1}_full_compatibility_level_1.png")
        
  end	

	
	def self.get_compact_table_for_this_table(tablename,compat_array)
      compat_tables = []
      for table_counter in 0..compat_array.flatten.length
          if compat_array[table_counter] == tablename
              compat_tables << compat_array[(table_counter-1)] 
          end	  
      end	  
      return compat_tables		
	end	
	

	def self.check_complete_db_compatibility(connection1,connection2)
      all_tables_under_one = connection1.tables
      all_tables_under_two = connection2.tables
      table_one_column = []
      table_two_column = []
      extra_tables = []
      compact_table_count = 0
      for column_one in all_tables_under_one	
          table_one_column << column_one
      end
      for column_two in all_tables_under_two	
          table_two_column << column_two
      end     
      if table_two_column.size != table_one_column.size
          if table_two_column.size > table_one_column.size
              puts "The databases are not compatible.Second database has #{(table_two_column.size)-(table_one_column.size)} extra tables."	
          elsif table_two_column.size < table_one_column.size
              puts "The databases are not compatible.First database  has #{(table_one_column.size)-(table_two_column.size)} extra tables."	
          end
      else
          table_count = table_two_column.size
          for table_one in table_one_column
              for table_two in table_two_column
                  compactness = check_compatibility(table_one,connection1,table_two,connection2)
                  if compactness 
                      compact_table_count = compact_table_count + 1
                  end	
              end
          end
      end	
      puts "Database profiling is complete.Please see the graphs for result."    
	end	

end	

